import string
from datetime import datetime
from time import strptime

class Type:
	
	def __init__(self):
		pass

	def get_type(self, ttype):

		if ttype in ['number', 'int', 'integer', 'int4', 'int8']:
			return TInteger()
		elif ttype in ['float', 'pointer', 'real']:
			return TFloat()
		elif ttype in ['string', 'text']:
			return TString()
		elif ttype in ['date', 'time', 'interval', 'realtime', 'timestamp', 'timestamptz', 'tinterval']:
			return TDate()
		elif ttype in ['bool', 'boolean']:
			return TBool()
		else:
			return TGeneric()

class Base:

	def __init__(self):

		self.name = ''

	def parse(self, value):
		return value

	def sqlquote(self, value):
		return repr(value)

	def encode(self, msg):
		
		try:
			text = unicode(msg, "ISO8859-1").encode("utf8")
		except:
			text = msg
		
		return text

	def format(self, value):
		return value

class TGeneric(Base):

	def __init__(self):
		Base.__init__(self)

class TBool(Base):

	def __init__(self):
		Base.__init__(self)

	def parse(self, value):

		if value or str(value).lower() in ['1', 't', 'true']:
			return True

		return False

	def sqlquote(self, value):

		if value or str(value).lower() in ['1', 't', 'true']:
			return 'true'

		return 'false'

class TInteger(Base):

	def __init__(self):
		Base.__init__(self)

	def parse(self, value):

		try:
			value = int(value)
		except:
			pass

		return value

	def sqlquote(self, value):

		return "'%s'" % ("%d" % value).replace("'", "\\'")

	def format(self, value):

		if value is None:
			return 0

		return self.parse(value)

class TFloat(Base):

	def __init__(self):
		Base.__init__(self)

	def parse(self, value):

		try:
			value = float(value)
		except:
			pass

		return value

	def sqlquote(self, value):

		return "'%f'" % value.replace("'", "\\'")

	def format(self, value):

		if value is None:
			return 0.0

		return self.parse(value)

class TString(Base):
	def __init__(self):
		Base.__init__(self)

	def parse(self, value):

		if value is None:
			return ''

		try:
			value = str(value)
		except:
			try:
				value = "%d" % value
			except:
				try:
					value = "%f" % value
				except:
					pass

		return self.encode(value)

	def sqlquote(self, value):

		if not value is None:
			return repr(value)

		return value

class TDateTime(Base):
	def __init__(self):
		Base.__init__(self)
		self.name = 'data'

	def sqlquote(self, value):

		value = str(value)
		try:
			value = datetime(*strptime(value, "%d/%m/%Y")[0:6]).strftime("%Y-%m-%d")
		except Exception, e:
			print "PARSE ERROR: %s" % e.__str__()
			try:
				value = datetime(*strptime(value, "%d/%m/%y")[0:6]).strftime("%Y-%m-%d")
			except Exception, e:
				print "PARSE ERROR: %s" % e.__str__()
				try:
					value = datetime(*strptime(value, "%d/%m/%Y %H:%M:%S")[0:6]).strftime("%Y-%m-%d %H:%M:%S")
				except Exception, e:
					print "PARSE ERROR: %s" % e.__str__()
					try:
						value = datetime(*strptime(value, "%d/%m/%y %H:%M:%S")[0:6]).strftime("%Y-%m-%d %H:%M:%S")
					except Exception, e:
						print "PARSE ERROR: %s" % e.__str__()
						pass

		return "'%s'" % value

	def parse(self, value):

		if value is None:
			return

		value = str(value)

		try:
			value = datetime(*strptime(value, "%Y-%m-%d")[0:6]).strftime("%d/%m/%Y")
		except Exception, e:
			print "PARSE ERROR: %s" % e.__str__()
			try:
				value = datetime(*strptime(value, "%y-%m-%d")[0:6]).strftime("%d/%m/%y")
			except Exception, e:
				print "PARSE ERROR: %s" % e.__str__()
				try:
					value = datetime(*strptime(value, "%Y-%m-%d %H:%M:%S")[0:6]).strftime("%d/%m/%Y %H:%M:%S")
				except Exception, e:
					print "PARSE ERROR: %s" % e.__str__()
					try:
						value = datetime(*strptime(value, "%y-%m-%d %H:%M:%S")[0:6]).strftime("%d/%m/%y %H:%M:%S")
					except Exception, e:
						print "PARSE ERROR: %s" % e.__str__()
						pass

		return value

	def format(self, value):
		return self.parse(value)

class TDate(Base):
	def __init__(self):
		Base.__init__(self)
		self.name = 'data'

	def sqlquote(self, value):

		value = str(value)
		try:
			value = datetime(*strptime(value, "%d/%m/%Y")[0:6]).strftime("%Y-%m-%d")
		except Exception, e:
			print "PARSE ERROR: %s" % e.__str__()
			try:
				value = datetime(*strptime(value, "%d/%m/%y")[0:6]).strftime("%Y-%m-%d")
			except Exception, e:
				print "PARSE ERROR: %s" % e.__str__()
				try:
					value = datetime(*strptime(value, "%d/%m/%Y %H:%M:%S")[0:6]).strftime("%Y-%m-%d")
				except Exception, e:
					print "PARSE ERROR: %s" % e.__str__()
					try:
						value = datetime(*strptime(value, "%d/%m/%y %H:%M:%S")[0:6]).strftime("%Y-%m-%d")
					except Exception, e:
						print "PARSE ERROR: %s" % e.__str__()
						pass

		return "'%s'" % value

	def parse(self, value):

		if value is None:
			return

		value = str(value)

		try:
			value = datetime(*strptime(value, "%Y-%m-%d")[0:6]).strftime("%d/%m/%Y")
		except Exception, e:
			print "PARSE ERROR: %s" % e.__str__()
			try:
				value = datetime(*strptime(value, "%y-%m-%d")[0:6]).strftime("%d/%m/%y")
			except Exception, e:
				print "PARSE ERROR: %s" % e.__str__()
				try:
					value = datetime(*strptime(value, "%Y-%m-%d %H:%M:%S")[0:6]).strftime("%d/%m/%Y")
				except Exception, e:
					print "PARSE ERROR: %s" % e.__str__()
					try:
						value = datetime(*strptime(value, "%y-%m-%d %H:%M:%S")[0:6]).strftime("%d/%m/%y")
					except Exception, e:
						print "PARSE ERROR: %s" % e.__str__()
						pass

		return value

	def format(self, value):
		return self.parse(value)

	def valida(self, value):

		v = str(value)

		try:
			datetime(*strptime(v, "%d/%m/%Y")[0:6])
		except Exception, e:
			print "VALIDA ERROR: %s" % e.__str__()
			return

		return value
